package com.zh.client.products.controller;

import com.zh.client.products.service.ProductsService;
import com.zh.client.user.service.UserService;
import com.zh.pojo.Notice;
import com.zh.pojo.Products;
import com.zh.pojo.User;
import com.zh.utils.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/client/product")
public class ProductsController {
    @Autowired
    ProductsService productsService;
    @Autowired
    UserService userService;

    @RequestMapping("/findproductByCategory")
    public String findproductByCategory(String category, Model model, @RequestParam(defaultValue = "1") int pageIndex, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("Login_user");
        if(user==null){
            user = autologin(request);
            if(user != null){
                request.getSession().setAttribute("Login_user",user);
            }
        }
        PageModel pageModel = new PageModel();
        pageModel.setPageIndex(pageIndex);
        List<Products> products = productsService.findproductByCategory(category,pageModel);

        int recordCount = productsService.findproductCountByCategory(category);
        pageModel.setRocordCount(recordCount);

        model.addAttribute("products",products);
        model.addAttribute("category",category);
        model.addAttribute("pageModel",pageModel);
//        for (Products product : products) {
//            System.out.println(product);
//        }
        return "client/product_list";
    }

    @RequestMapping("/product_search_list")
    public String product_search_list(String name,Model model, @RequestParam(defaultValue = "1") int pageIndex, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("Login_user");
        if(user==null){
            user = autologin(request);
            if(user != null){
                request.getSession().setAttribute("Login_user",user);
            }
        }
        PageModel pageModel = new PageModel();
        pageModel.setPageIndex(pageIndex);
        List<Products> products = productsService.findproductByname(name,pageModel);
        int recordCount = productsService.findproductByNameCount(name);
        pageModel.setRocordCount(recordCount);
//        for (Products product : products) {
//            System.out.println(product);
//        }
        model.addAttribute("pageModel",pageModel);
        model.addAttribute("products",products);
        model.addAttribute("name",name);

        return "client/product_search_list";
    }

    //自动登录
    public User autologin(HttpServletRequest request) {
        String username = null;
        String password = null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if("bookstore_username".equals(cookie.getName())){
                username = cookie.getValue();
            }
            if("bookstore_password".equals(cookie.getName())){
                password = cookie.getValue();
            }
        }
        User user = new User();
        user.setPassword(password);
        user.setUsername(username);
        return userService.findUserByLogin(user);
    }

    @RequestMapping("/findProductById")
    public String findProductById(String id,Model model){
        Products product = productsService.findProductById(id);
        model.addAttribute("p",product);
        return "client/info";
    }


    @RequestMapping("/showIndex")
    public String showIndex(Model model){
        Notice notice = productsService.findNotice();
        model.addAttribute("n",notice);
        List<Products> products = productsService.findWeekHotProduct();
        model.addAttribute("products",products);
        return "client/index";
    }
}
